/*
 * @Description: 移交登记reducer
 * @Author: Rfan
 * @Date: 2022-11-23 17:50:24
 * @LastEditTime: 2022-11-24 15:45:05
 */

import {
  ADD_REG_BORROW,
  DELETE_REGISTRATION_ITEM,
  EDIT_REG_BORROW,
  SET_REG_BORROW_LIST,
  SET_SELECT_AT,
  SET_SELECT_AT_ID,
  SET_SELECT_FOND_ID,
  SET_SELECTED_ITEM
} from '../constants';
import { delItemByIdArray, editItemById, insertItem, insertItemAfter } from '@/utils/list';

export const BorrowReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中的全宗
     */
    case SET_SELECT_FOND_ID:
      return {
        ...state,
        selectFondId: action.id
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT_ID: {
      return {
        ...state,
        selectATId: action.id
      };
    }

    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 设置移交登记列表
     */
    case SET_REG_BORROW_LIST: {
      return {
        ...state,
        list: action.data
      };
    }

    /**
     * @description: 添加移交登记条目
     */
    case ADD_REG_BORROW: {
      return {
        ...state,
        list: insertItemAfter(action.data, state.list)
      };
    }

    /**
     * @description: 编辑移交登记条目
     */
    case EDIT_REG_BORROW: {
      const { id, data } = action;
      return {
        ...state,
        list: editItemById(id, data, state.list)
      };
    }

    /**
     * @description: 删除待处置列表项目
     */
    case DELETE_REGISTRATION_ITEM: {
      return {
        ...state,
        list: delItemByIdArray(action.ids, state.list)
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
